package com.inforich.web.controller.projectManage;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Arrays;
import cn.hutool.core.bean.BeanUtil;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;
import com.inforich.common.annotation.Log;
import com.inforich.common.core.controller.BaseController;
import com.inforich.common.core.domain.AjaxResult;
import com.inforich.common.enums.BusinessType;
import com.inforich.projectManage.domain.Project;
import com.inforich.projectManage.service.IProjectService;
import com.inforich.common.utils.poi.ExcelUtil;

/**
 * 项目Controller
 *
 * @author hjm
 * @date 2022-10-22
 */
@RestController
@RequestMapping("/projectManage/project")
public class ProjectController extends BaseController {

  @Autowired
  private IProjectService projectService;

  /**
   * 所有项目
   */
  @GetMapping("/all")
  public AjaxResult all(Project project) {
    Map<String, Object> map = beanToMap(project);
    List<Project> list = projectService.listProject(map);
    return AjaxResult.success(list);
  }

  /**
   * 查询项目列表
   */
  @PreAuthorize("@ss.hasPermi('projectManage:project:list')")
  @GetMapping("/list")
  public Page<Project> list(Page<Project> page, Project project) {
    Map<String, Object> map = beanToMap(project);
    projectService.pageProject(page, map);
    return page;
  }


  /**
   * 导出项目列表
   */
  @PreAuthorize("@ss.hasPermi('projectManage:project:export')")
  @Log(title = "项目", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(HttpServletResponse response, Project project) {
    Map<String, Object> map = beanToMap(project);
    List<Project> list = projectService.listProject(map);
    ExcelUtil<Project> util = new ExcelUtil<Project>(Project.class);
    util.exportExcel(response, list, "项目数据");
  }

  /**
   * 获取项目详细信息
   */
  @PreAuthorize("@ss.hasPermi('projectManage:project:query')")
  @GetMapping(value = "/{id}")
  public AjaxResult getInfo(@PathVariable("id") String id) {
    return AjaxResult.success(projectService.getProjectById(id));
  }

  /**
   * 新增项目
   */
  @PreAuthorize("@ss.hasPermi('projectManage:project:add')")
  @Log(title = "项目", businessType = BusinessType.INSERT)
  @PostMapping
  public AjaxResult add(@Validated @RequestBody Project project) {
    String checkData = projectService.checkData(project);
    if (StrUtil.isNotBlank(checkData)) {
      return AjaxResult.error(checkData);
    }
    return toAjax(projectService.insertProject(project));
  }

  /**
   * 修改项目
   */
  @PreAuthorize("@ss.hasPermi('projectManage:project:edit')")
  @Log(title = "项目", businessType = BusinessType.UPDATE)
  @PutMapping
  public AjaxResult edit(@Validated @RequestBody Project project) {
    String checkData = projectService.checkData(project);
    if (StrUtil.isNotBlank(checkData)) {
      return AjaxResult.error(checkData);
    }
    return toAjax(projectService.updateProject(project));
  }

  /**
   * 删除项目
   */
  @PreAuthorize("@ss.hasPermi('projectManage:project:remove')")
  @Log(title = "项目", businessType = BusinessType.DELETE)
  @DeleteMapping("/{ids}")
  public AjaxResult remove(@PathVariable String[] ids) {
    return toAjax(projectService.removeByIds(Arrays.asList(ids)));
  }

  /**
   * 资源审批
   */
  @PreAuthorize("@ss.hasPermi('projectManage:project:approveResource')")
  @PostMapping("/approveResource")
  public AjaxResult approveResource(@RequestBody JSONObject param) {
    return projectService.approveResource(param);
  }

  /**
   * 资源审批
   */
  @PreAuthorize("@ss.hasPermi('projectManage:project:approveProject')")
  @PostMapping("/approveProject")
  public AjaxResult approveProject(@RequestBody JSONObject param) {
    return projectService.approveProject(param);
  }
}
